import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.border.EmptyBorder;
import javax.swing.JComboBox;


@SuppressWarnings("serial")
public class PatientTreatmentDialogAdmin extends JDialog {

	/**
	 * Holds the content for the panel.
	 */ 
	private final JPanel contentPanel = new JPanel();
	
	/**
	 * First name of nurse
	 */ 
	private final JTextField txtNursefirstname;
	
	/**
	 * Last name of nurse
	 */ 
	private final JTextField txtNurselastname;
	
	/**
	 * Recorded weight
	 */ 
	private final JTextField txtWeight;
	
	/**
	 * Recorded height
	 */ 
	private final JTextField txtHeight;
	
	/**
	 * Recorded blood pressure
	 */ 
	private final JTextField txtBloodPressure;
	
	/**
	 * Recorded pulse
	 */ 
	private final JTextField txtPulse;
	
	/**
	 * Recorded temperature
	 */ 
	private final JTextField txtTemperature;
	
	/**
	 * Recorded diagnosis
	 */ 
	private final JTextArea txtDiagnosis;
	
	/**
	 * Recorded symptoms
	 */ 
	private final JTextArea txtSymptoms;
	
	/**
	 * Doctor SSN
	 */ 
	private final int drSSN;
	
	/**
	 * Database connection
	 */ 
	private Connection con;
	
	/**
	 * OK Button
	 */ 
	private final JButton okButton;
	
	/**
	 * Pane that holds the buttons
	 */ 
	private final JPanel buttonPane;
	
	/**
	 * Cancel button
	 */ 
	private final JButton cancelButton;
	
	/**
	 * Recorded blood pressure
	 */ 
	private final JTextField txtBloodPressure_D;
	
	/**
	 * Frame that supports all dialog windows
	 */ 
	private JFrame frame;

	/**
	 * First name of doctor
	 */ 
	private String doctorFirstName;

	/**
	 * Last name of doctor
	 */ 
	private String doctorLastName;

	/**
	 * Patient height
	 */ 
	protected double height;

	/**
	 * Patient weight
	 */ 
	protected double weight;

	/**
	 * Systolic blood pressure
	 */ 
	protected int bloodPressure_S;

	/**
	 * Diastolic blood pressure
	 */ 
	protected int bloodPressure_D;

	/**
	 * Recorded pulse
	 */ 
	protected int pulse;

	/**
	 * Recorded temperature
	 */ 
	protected double temperature;

	private JComboBox cmbNurseSSN;

	private Integer nurseSSN;

	/**
	 * Create the dialog.
	 */
	public PatientTreatmentDialogAdmin(final MedAppController mac, final int ssn, final int drSSN) {
		setTitle("Add Patient Treatment");
		con = mac.getCon();
		this.drSSN = drSSN;
		
		String sql = "SELECT * FROM `doctor` WHERE `D_SSN`= '" + drSSN + "'";
		String doctorName = "";
		try {
			Statement stmt = con.createStatement();
			ResultSet rs = stmt.executeQuery(sql);
			
			while(rs.next()){
				doctorFirstName = rs.getString(1);
				doctorLastName=rs.getString(2);
			}
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		setResizable(false);
		setBounds(100, 100, 792, 500);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(null);
		{
			final JLabel lblTreatingDoctor = new JLabel("Treating Doctor SSN");
			lblTreatingDoctor.setBounds(10, 23, 106, 14);
			contentPanel.add(lblTreatingDoctor);
		}
		{
			final JLabel lblAttendingNurse = new JLabel("Attending Nurse:");
			lblAttendingNurse.setBounds(10, 64, 118, 14);
			contentPanel.add(lblAttendingNurse);
		}
		{
			txtNursefirstname = new JTextField();
			txtNursefirstname.setBounds(128, 78, 88, 22);
			contentPanel.add(txtNursefirstname);
			txtNursefirstname.setColumns(10);
		}
		{
			txtNurselastname = new JTextField();
			txtNurselastname.setBounds(275, 78, 88, 22);
			contentPanel.add(txtNurselastname);
			txtNurselastname.setColumns(10);
		}
		{
			final JLabel lblSymptoms = new JLabel("Symptoms");
			lblSymptoms.setBounds(10, 141, 86, 14);
			contentPanel.add(lblSymptoms);
		}
		{
			txtSymptoms = new JTextArea();
			txtSymptoms.setLineWrap(true);
			txtSymptoms.setWrapStyleWord(true);
			txtSymptoms.setBounds(126, 141, 238, 36);
			contentPanel.add(txtSymptoms);
		}
		{
			final JLabel lblBloodPressure = new JLabel("Blood Pressure");
			lblBloodPressure.setBounds(10, 304, 86, 14);
			contentPanel.add(lblBloodPressure);
		}
		{
			final JLabel lblPulse = new JLabel("Pulse");
			lblPulse.setBounds(10, 349, 46, 14);
			contentPanel.add(lblPulse);
		}
		{
			final JLabel lblTemperature = new JLabel("Temperature");
			lblTemperature.setBounds(10, 394, 86, 14);
			contentPanel.add(lblTemperature);
		}
		{
			final JLabel lblHeight = new JLabel("Height");
			lblHeight.setBounds(10, 214, 46, 14);
			contentPanel.add(lblHeight);
		}
		{
			final JLabel lblWeight = new JLabel("Weight");
			lblWeight.setBounds(10, 259, 46, 14);
			contentPanel.add(lblWeight);
		}
		{
			txtWeight = new JTextField();
			txtWeight.setBounds(128, 255, 88, 22);
			contentPanel.add(txtWeight);
			txtWeight.setColumns(10);
		}
		{
			txtHeight = new JTextField();
			txtHeight.setBounds(128, 210, 88, 22);
			contentPanel.add(txtHeight);
			txtHeight.setColumns(10);
		}
		{
			txtBloodPressure = new JTextField();
			txtBloodPressure.setBounds(128, 300, 88, 22);
			contentPanel.add(txtBloodPressure);
			txtBloodPressure.setColumns(10);
		}
		{
			txtPulse = new JTextField();
			txtPulse.setBounds(128, 345, 88, 22);
			contentPanel.add(txtPulse);
			txtPulse.setColumns(10);
		}
		{
			txtTemperature = new JTextField();
			txtTemperature.setBounds(128, 390, 88, 22);
			contentPanel.add(txtTemperature);
			txtTemperature.setColumns(10);
		}
		{
			final JLabel lblDiagnosis = new JLabel("Diagnosis");
			lblDiagnosis.setBounds(448, 68, 100, 14);
			contentPanel.add(lblDiagnosis);
		}
		{
			txtDiagnosis = new JTextArea();
			txtDiagnosis.setLineWrap(true);
			txtDiagnosis.setWrapStyleWord(true);
			txtDiagnosis.setBounds(458, 103, 318, 313);
			contentPanel.add(txtDiagnosis);
		}
		{
			final JLabel lblLast = new JLabel("Last");
			lblLast.setHorizontalAlignment(SwingConstants.RIGHT);
			lblLast.setBounds(226, 82, 39, 14);
			contentPanel.add(lblLast);
		}
		{
			final JLabel lblFirst = new JLabel("First");
			lblFirst.setBounds(92, 82, 46, 14);
			contentPanel.add(lblFirst);
		}
		
		txtBloodPressure_D = new JTextField();
		txtBloodPressure_D.setBounds(277, 301, 86, 20);
		contentPanel.add(txtBloodPressure_D);
		txtBloodPressure_D.setColumns(10);
		
		final JLabel lblS = new JLabel("S");
		lblS.setBounds(219, 304, 46, 14);
		contentPanel.add(lblS);
		
		final JLabel lblD = new JLabel("D");
		lblD.setBounds(368, 304, 46, 14);
		contentPanel.add(lblD);
		
		JLabel lblSsn = new JLabel("SSN:");
		lblSsn.setHorizontalAlignment(SwingConstants.RIGHT);
		lblSsn.setBounds(82, 116, 46, 14);
		contentPanel.add(lblSsn);
		
		final String sql3 = "SELECT `D_SSN` FROM `doctor";
		final ArrayList<Integer> doctorSSNs = new ArrayList<Integer>();
		try{
			Statement stmt = con.createStatement();
			ResultSet rs = stmt.executeQuery(sql3);
			
			while(rs.next()){
				doctorSSNs.add(rs.getInt(1));
			}
		}catch(SQLException e){
			e.printStackTrace();
		}
		
		Object[] array1 = doctorSSNs.toArray();
		JComboBox comboBox = new JComboBox(array1);
		comboBox.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				
			}
		});
		comboBox.setBounds(125, 20, 238, 22);
		contentPanel.add(comboBox);
		
		final String sql2 = "SELECT `N_SSN` FROM `nurse";
		final ArrayList<Integer> nurseSSNs = new ArrayList<Integer>();
		try{
			Statement stmt = con.createStatement();
			ResultSet rs = stmt.executeQuery(sql2);
			
			while(rs.next()){
				nurseSSNs.add(rs.getInt(1));
			}
		}catch(SQLException e){
			e.printStackTrace();
		}
		
		Object[] array = nurseSSNs.toArray();
		cmbNurseSSN = new JComboBox(array);
		cmbNurseSSN.setSelectedIndex(0);
		nurseSSN = (Integer) cmbNurseSSN.getSelectedItem();
		cmbNurseSSN.setBounds(128, 110, 238, 20);
		contentPanel.add(cmbNurseSSN);
		{
			buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				okButton = new JButton("OK");
				okButton.addActionListener(new ActionListener() {
					private String treatingDoctor;

					public void actionPerformed(ActionEvent arg0) {
						final String attendingNurseFName = txtNursefirstname.getText();
						final String attendingNurseLName = txtNurselastname.getText();
						final String symptoms = txtSymptoms.getText();
//						final int nurseSSN = Integer.parseInt(txtNurseSSN.getText());
						try {
							height = Double.parseDouble(txtHeight.getText());
						} catch (Exception e){
							JOptionPane.showMessageDialog(frame,
								    "Please check your \"height\" input.",
								    "Input Error",
								    JOptionPane.WARNING_MESSAGE);
						} //end try-catch block
						try {
							weight = Double.parseDouble(txtWeight.getText());
						} catch (Exception e){
							JOptionPane.showMessageDialog(frame,
								    "Please check your \"weight\" input.",
								    "Input Error",
								    JOptionPane.WARNING_MESSAGE);
						} //end try-catch block
						try {
							bloodPressure_S = Integer.parseInt(txtBloodPressure.getText());
						} catch (Exception e){
							JOptionPane.showMessageDialog(frame,
								    "Please check your \"Blood Pressure\" input.",
								    "Input Error",
								    JOptionPane.WARNING_MESSAGE);
						} //end try-catch block
						try {
							bloodPressure_D = Integer.parseInt(txtBloodPressure_D.getText());
						} catch (Exception e){
							JOptionPane.showMessageDialog(frame,
								    "Please check your \"Blood Pressure\" input.",
								    "Input Error",
								    JOptionPane.WARNING_MESSAGE);
						} //end try-catch block
						try {
							pulse = Integer.parseInt(txtPulse.getText());
						} catch (Exception e){
							JOptionPane.showMessageDialog(frame,
								    "Please check your \"pulse\" input.",
								    "Input Error",
								    JOptionPane.WARNING_MESSAGE);
						} //end try-catch block
						try {
							temperature = Double.parseDouble(txtTemperature.getText());
						} catch (Exception e){
							JOptionPane.showMessageDialog(frame,
								    "Please check your \"Temperature\" input.",
								    "Input Error",
								    JOptionPane.WARNING_MESSAGE);
						} //end try-catch block
						final String diagnosis = txtDiagnosis.getText();

						String sql = "INSERT INTO `velociraptorsattack`.`treatmentrecord` (`T_DOCTORFIRSTNAME`, `T_DOCTORLASTNAME`, `T_DATEANDTIME`, `T_NURSEFIRSTNAME`, `T_NURSELASTNAME`, `T_SYMPTOMS`, `T_BP_S`, `T_BP_D`, `T_PULSE`, `T_TEMPERATURE`, `T_HEIGHT`, `T_WEIGHT`, `T_DIAGNOSIS`, `T_FORPATIENTSSN`, `T_NURSESSN`, `T_DOCTORSSN`,`T_ID`) VALUES ('" + doctorFirstName + "', '" + doctorLastName + "', NOW(), '" + attendingNurseFName + "', '" + attendingNurseLName + "', '" + symptoms + "', '" + bloodPressure_S + "', '" + bloodPressure_D + "', '" + pulse + "', '" + temperature + "', '" + height + "', '" + weight + "', '" + diagnosis + "', '" + ssn + "', '123456788', '" + drSSN + "','" + mac.getNextT_ID() + "')";					
						mac.out.println(sql);
						try {
							Statement stmt = con.createStatement();
							int result = stmt.executeUpdate(sql);
							JOptionPane.showMessageDialog(null,"Treatment Record Added!");
						} catch (SQLException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
						getRootPane().setVisible(false);
						dispose();
					}
				});
				okButton.setActionCommand("OK");
				buttonPane.add(okButton);
				getRootPane().setDefaultButton(okButton);
			}
			{
				cancelButton = new JButton("Cancel");
				cancelButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent arg0) {
						getRootPane().setVisible(false);
						dispose();
					}
				});
				cancelButton.setActionCommand("Cancel");
				buttonPane.add(cancelButton);
			}
		}
	}
}
